package de.java.shortys.myhome.api.view;

import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGMODULEVIEW_BACKGROUND;
import static java.awt.Adjustable.HORIZONTAL;
import static java.awt.Adjustable.VERTICAL;
import static java.awt.BorderLayout.NORTH;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import de.java.shortys.myhome.viewconfigurator.controller.ViewConfigurationManager;

public class SwingModuleView implements IView {

	private JPanel component;

	private JPanel contentPane;

	private DefaultMutableTreeNode node;

	private SwingModuleHead head;

	private JPanel body;

	private SwingModuleViewTree subModuleTree;

	private JScrollPane treeScrollPane;

	private int oldHeight = 0;

	public SwingModuleView() {
		initialize();
	}

	public DefaultMutableTreeNode add(String title) {
		node = new DefaultMutableTreeNode(title);
		subModuleTree.add(node);
		increaseBodySize(subModuleTree.getRowHeight());

		return node;
	}

	public void addChild(String title) {
		node.add(new DefaultMutableTreeNode(title));
		increaseBodySize(subModuleTree.getRowHeight());
		subModuleTree.reload();
	}

	public void addCloseListener(ActionListener listener) {
		head.addCloseListener(listener);
	}

	public void addExpandListener(ActionListener listener) {
		head.addExpandListener(listener);
	}

	public void addTreeClickListener(MouseListener listener) {
		subModuleTree.addMouseListener(listener);
	}

	@Override
	public Component build() {
		return component;
	}

	@Override
	public void clear() {
		// Empty
	}

	public void collapse() {
		oldHeight = body.getPreferredSize().height;
		body.setPreferredSize(new Dimension(0, 0));
		head.setExpanded(false);
		body.revalidate();
		body.repaint();
	}

	private void createBody() {
		body = new JPanel();
		body.setPreferredSize(new Dimension(0, 0));
		body.setOpaque(false);
		body.setLayout(new BorderLayout());
		contentPane.add(body);

	}

	private void createComponent() {
		component = new JPanel();
		component.setOpaque(false);
		component.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
		component.setLayout(new BorderLayout());
	}

	private void createContentPane() {
		contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		contentPane.setBackground(ViewConfigurationManager.getInstance().getColor(SWINGMODULEVIEW_BACKGROUND));
		contentPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory
				.createLineBorder(new Color(101, 147, 207)), BorderFactory.createEmptyBorder(1, 1, 1, 1)));
		component.add(contentPane);
	}

	private void createHead() {
		head = new SwingModuleHead();
		contentPane.add(BorderLayout.NORTH, head);
	}

	private void createTree() {
		subModuleTree = new SwingModuleViewTree();
		treeScrollPane = new JScrollPane(subModuleTree);
		treeScrollPane.setMinimumSize(new Dimension(0, 0));
		treeScrollPane.setPreferredSize(new Dimension(0, 0));
		treeScrollPane.setBorder(null);
		treeScrollPane.setOpaque(false);
		treeScrollPane.getVerticalScrollBar().setUI(new SwingModuleViewScrollBarUI(VERTICAL));
		treeScrollPane.getHorizontalScrollBar().setUI(new SwingModuleViewScrollBarUI(HORIZONTAL));
		treeScrollPane.getViewport().setOpaque(false);
		body.add(treeScrollPane);

	}

	public void setExtendedView(IView view) {
		Component build = view.build();
		increaseBodySize(build.getPreferredSize().height);
		build.setMinimumSize(new Dimension(0, 0));
		body.add(NORTH, build);
	}

	public void expand() {
		body.setPreferredSize(new Dimension(0, oldHeight));
		head.setExpanded(true);
		body.revalidate();
		body.repaint();
	}

	public DefaultMutableTreeNode getNode() {
		return node;
	}

	public String getSelectedUserObject() {
		TreePath selectionPath = subModuleTree.getSelectionPath();
		if (selectionPath != null) {
			return selectionPath.getLastPathComponent().toString();
		}

		return null;
	}

	public String getTitle() {
		return head.getTitle();
	}

	private void increaseBodySize(int increasement) {
		body.setPreferredSize(new Dimension(0, body.getPreferredSize().height + increasement));
	}

	private void initialize() {

		createComponent();
		createContentPane();
		createHead();
		createBody();
		createTree();
		component.revalidate();
	}

	public void setDeactivatable(boolean deactivatable) {
		head.setDeactivatable(deactivatable);
	}

	public void setTitle(String title) {
		head.setTitle(title);
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}
}
